package com.pw.study.structure.stack;

/**
 * @description: 模拟栈
 * 1.定义初始化
 * 2.压入数据
 * 3.弹出数据
 * @author: linux_future
 * @date: 2022/11/25
 */
public class FutureArrayStack {
    private int maxSize;
    private int[] stack;
    private int top = -1;

    /**
     * 初始化栈
     *
     * @param maxSize
     */
    public FutureArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    /**
     * 栈顶元素
     *
     * @return
     */
    public int peek() {
        return stack[top];
    }

    /**
     * 判断栈满
     *
     * @return
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 判断栈空
     *
     * @return
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 添加元素
     *
     * @param value
     */
    public void push(int value) {
        if (isFull()) {
            System.out.println("这个栈已经满了。。。。。。");
            return;
        }
        top++;
        stack[top] = value;
    }

    /**
     * 弹出元素
     *
     * @return
     */
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("数据栈已经为空。。。。。。。。");
        }
        int value = stack[top];
        top--;
        return value;
    }

    /**
     * 遍历栈元素
     */
    public void list() {
        if (isEmpty()) {
            System.out.println("数据栈空，没有数据。。。。。。。。。。。");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println("数据元素:" + i + "" + stack[i]);
        }
    }

    /**
     * 数据计算
     *
     * @param num1
     * @param num2
     * @param ch
     * @return
     */
    public int cal(int num1, int num2, char ch) {
        int result = 0;
        switch (ch) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 设置优先级别
     *
     * @param ch
     */
    public int priority(char ch) {

        if (ch == '*' || ch == '?') {
            return 1;
        } else if (ch == '+' || ch == '-') {
            return 0;
        } else {
            return -1;
        }
    }
}
